<?php
namespace app\controller\model\apis;

use app\BaseController;
use app\controller\model;
use GuzzleHttp\Client;
use think\facade\Db;
class ApiPanda extends BaseController
{
	public $urlTest="";
	public $develop=false;
	public $apiUrl="";
	protected $url='',$appKey,$secret,$supplierCode,$tables,$token;
	public $supplier;
	protected $header=["Content-Type" => "application/json"];
    public function __construct($supplier=[]){
	    $this->tables = new model\Tables();
		$this->start($supplier);
    }
	//熊猫加油的所有接口
	public function cancelOrder($supplier_no){
		return true;
	}
	public function mkOrder($order){
		return true;
	}
	public function getGas($orderID){
		return true;
	}
	public function returnOrder($order){
		return true;
	}
	public function confirmOrder($supplier_no){
		return true;
	}
	public function getGasList(){
		try {
			$api = "api/v1/station?token=$this->token&code=$this->supplierCode";
			var_dump($api);
			$re = $this->sendUrl($api,['page_no'=>1,'rows'=>10]);
			var_dump($re);
			if($re && $re['code']==200){
				foreach ($re['data'] as $data){
					lock();
					$price = $data['price'];
					unset($data['price']);
					$gas = DB::name($this->tables->gas)->lock(true)->where("gasID",$data['gasID'])->find();
					if($gas){
						DB::name($this->tables->gas)->where("id",$gas['id'])->update($data);
						$gasID = $gas['id'];
					}else{
						$gasID = DB::name($this->tables->gas)->insertGetId($data);
					}
					if($gasID){
						foreach ($price as $v){
							$v['gun_nos'] = json_encode($v['gun_nos']);
							$price = DB::name($this->tables->gasPrice)->lock(true)->where("gasID",$gasID)->where("oil_no",$v['oil_no'])->find();
							if($price){
								DB::name($this->tables->gasPrice)->where("id",$price['id'])->update($v);
							}else{
								DB::name($this->tables->gasPrice)->insertGetId($v);
							}
						}
					}
					unlockCommit();
				}
			}else{
				return false;
			}
		} catch (\Exception $e) {
			unlockRollback();
			return ['error' => 1, "message" => $e->getMessage()];
		}

	}
	protected function getToken(){
		$api = "api/v1/token";
		$re = $this->sendUrl($api,['channel_code'=>$this->appKey,'channel_key'=>$this->secret]);
		if($re && $re['code']==200){
			$this->supplier['token']=$re['data']['token'];
			$this->supplier['tokenTime']=time() + 7200;
			DB::name($this->tables->supplier)->update($this->supplier);
			return true;
		}else{
			return false;
		}
	}

	protected function start($supplier=[]){
		$this->supplier = $supplier;
		$this->apiUrl = $this->supplier['apiUrl'];
		$this->urlTest = $this->supplier['apiUrlTest'];
		$this->develop?$this->url = $this->apiUrl:$this->url = $this->urlTest;
		$this->url = trim($this->url,"/")."/";
		$this->appKey = $this->supplier['appKey'];
		$this->secret = $this->supplier['secret'];
		$this->supplierCode = $this->supplier['supplierCode'];
		if($this->supplier['postType']==2){
			if($this->supplier['tokenTime']<time()){
				$this->getToken();
			}else{
				$this->token = $this->supplier['token'];
			}
		}
	}
	protected function sendUrl($url,$json='',$header=["Content-Type" => "application/json"]){
		$url = $this->url.$url;
		$client = new Client([
			"base_uri" => $url,
			"headers" => $header,
			"verify" => false,
		]);
		try {
			$data = $client->post($url,[
				"json" => ['params2' => json_encode($json)]
			]);
			$content = $data->getBody()->getContents();
			$content = json_decode($content, true);
			if (empty($content) ) throw new \Exception("请求失败");
			if($content['code'] != 200) throw new \Exception($content['msg']);
			return $content;
		} catch (\Exception $e) {
			return ['error' => 1, "message" => $e->getMessage()];
		}
	}
}
?>